// 声明状态为商品列表，是个空数组
const state = {
  goodsList: uni.getStorageSync('cart') || [],
};
// 声明变化，里面是各个函数
const mutations = {
  // 添加商品到购物车
  addGoods(state, goodsData) {
    const indexFound = state.goodsList.findIndex(
      (item) => Number(item.goods_id) === Number(goodsData.goods_id)
    );
    if (indexFound === -1) {
      // 新增
      state.goodsList.push(goodsData);
    } else {
      // 追加
      state.goodsList = state.goodsList.map((item, index) => {
        if (index === indexFound) {
          return { ...item, number: item.number + 1 };
        } else {
          return { ...item };
        }
      });
    }
    uni.setStorageSync('cart', state.goodsList);
  },
  // 设置购物车内商品的数量
  setGoodsNumber(state, payload) {
    const { goods_id, number } = payload;
    const indexFound = state.goodsList.findIndex(
      (i) => i.goods_id === goods_id
    );
    state.goodsList[indexFound].number = number;
    uni.setStorageSync('cart', state.goodsList);
  },
  // 删除购物车内商品的数量
  removeGoods(state, goods_id) {
    const indexFound = state.goodsList.findIndex(
      (i) => i.goods_id === goods_id
    );
    state.goodsList.splice(indexFound, 1);
    uni.setStorageSync('cart', state.goodsList);
  },
  // 购物车内的商品选中情况
  toggleCheck(state, goods_id) {
    const indexFound = state.goodsList.findIndex(
      (i) => i.goods_id === goods_id
    );
    state.goodsList[indexFound].isChecked =
      !state.goodsList[indexFound].isChecked;
    uni.setStorageSync('cart', state.goodsList);
  },
  // 购物车内全选的逻辑
  toggleAllChecked(state) {
    const isAllChecked =
      state.goodsList.length > 0 &&
      state.goodsList.every((item) => item.isChecked);
    let isChecked;
    if (isAllChecked) {
      isChecked = false;
    } else {
      isChecked = true;
    }
    state.goodsList.forEach((element) => {
      element.isChecked = isChecked;
    });
    uni.setStorageSync('cart', state.goodsList);
  },
  clearCartAfterPayment(state) {
    state.goodsList = state.goodsList.filter((item) => !item.isChecked);
    uni.setStorageSync('cart', state.goodsList);
  },
};
const actions = {};
const getters = {
  // 商品品类数目
  goodsCount: (state) => {
    let res = 0;
    state.goodsList.forEach((element) => {
      if (element.isChecked) {
        res++;
      }
    });
    return res;
  },
  // 全选的计算
  isAllChecked: (state) => {
    return (
      state.goodsList.length > 0 &&
      state.goodsList.every((item) => item.isChecked)
    );
  },
  // 商品总数量
  totalGoodsNumber: (state) => {
    let total = 0;
    state.goodsList.forEach((element) => {
      if (element.isChecked) {
        total += element.number;
      }
    });
    return total;
  },
  // 选中的商品总价格
  totalPrice: (state) => {
    let total = 0;
    state.goodsList.forEach((element) => {
      if (element.isChecked) {
        total += element.goods_price * element.number;
      }
    });
    return total;
  },
  // 商品列表中被选中的商品
  checkedGoodsList: (state) => state.goodsList.filter((item) => item.isChecked),
};

export default { namespaced: true, state, mutations, actions, getters };
